package com.test;

import java.awt.*;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;

public class FontUtil {
	/**
     * 读取字体文件
     * @param fontName
     * @param style
     * @param size
     * @return
     * @throws IOException
     */
    public static Font getFont(String fontName,Integer style,float size){
    	InputStream stream = FontUtil.class.getClassLoader().getResourceAsStream(fontName);
        BufferedInputStream bis = new BufferedInputStream(stream);
        Font font = null;
        try {
            font = Font.createFont(Font.TRUETYPE_FONT, bis);
            if(style != null) {
            	font = font.deriveFont(style,size);
            }else {
            	font = font.deriveFont(size);
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        	try {
        		if(bis != null)
        			bis.close();
        		if(stream != null)
        			stream.close();
        	}catch(Exception e) {
        	}
        }
        return font;
    }
}
